""" divmod函数
快慢指针
 """
def isHappy(n: int) -> bool:
    def get_next(number):
        total_sum = 0 
        while number >0:
            number,digit = divmod(number, 10)
            total_sum += digit**2
        return total_sum
    
    # 利用set判断有没有出现循环
    # seen = set()
    # while n != 1 and n not in seen:
    #     seen.add(n)
    #     n = get_next(n)
    # return n == 1

    # 快慢指针
    fast, slow = get_next(n), n

    # 终止条件：
    # fast到达1 或者slow和fast相等
    while fast != 1 and fast != slow: 
        fast = get_next(get_next(fast))
        slow = get_next(slow)

    return fast == 1  